package yu.zhu.top.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import yu.zhu.top.pojo.Movie;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author yuzhu
 * @Date 2020/11/16 22:17
 */
@Service
public class CustomMultiThreadingService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    private Logger logger = LoggerFactory.getLogger(CustomMultiThreadingService.class);
    @Async
    public  void TOP250() throws IOException {
        logger.info("多线程版本 ==> 任务开始 method: TOP250{} ");

        String s1 = "https://movie.douban.com/top250?start=";
        long l = System.nanoTime();
        // 存储待爬取的网址url链接
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i <= 250; i += 25) {
            list.add(s1+i);
        }
        // 遍历url集合 爬取网页数据
        for (String string : list) {
            Document doc = Jsoup.connect(string).userAgent
                    ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:60.0) Gecko/20100101 Firefox/60.0").
                    timeout(6000).get();
            Element content = doc.getElementById("content");
            Elements infos = content.getElementsByClass("info");
            for (Element element : infos) {
                Movie movie = new Movie();
                Element links = element.getElementsByTag("a").get(0);
                Element star = element.getElementsByClass("star").get(0);
                // 获取电影的链接
                movie.setLink(links.attr("href"));
                // 获取电影的名称
                movie.setTitle(links.child(0).html());
                // 获取电影的评分
                movie.setScore(star.child(1).html());
                // 获取电影的评价数
                String html = star.child(3).html();
                html = html.replaceAll("人评价", "");
                movie.setNum(html);
                //System.out.println(movie.getLink() + "\t" + movie.getTitle() + "\t评分" + movie.getScore() + "\t" + movie.getNum());
                pushToMq(movie);
            }
        }
    }
    private void pushToMq(Movie movie) {
        Map<String,Object> map=new HashMap<>();
        map.put("link", movie.getLink());
        map.put("title", movie.getTitle());
        map.put("score", movie.getScore());
        map.put("num", movie.getNum());
        //将消息携带绑定键值：top-spiders 发送到交换机top-spiders-Exchange
        rabbitTemplate.convertAndSend("top_spiders_Exchange", "", map);

    }

}
